package com.nc.delivery_project.server.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.nc.delivery_project.server.domain.Country;
import com.nc.delivery_project.server.domain.Role;
import com.nc.delivery_project.server.domain.StateProvince;
import com.nc.delivery_project.server.domain.User;

public class StateProvinceDao  extends AbstractDao<StateProvince>{

	protected final static String ENTITY_NAME = StateProvince.class.getSimpleName();

	protected final static String STATE_ID = "STATE_ID";
	protected final static String STATE_TITLE = "STATE_TITLE";
	protected final static String COUNTRY_ID = "COUNTRY_ID";
	
	
	public StateProvinceDao(Connection connection) throws SQLException {
		super(connection,ENTITY_NAME);
		
	}
	
	public StateProvinceDao() {
		super(ENTITY_NAME);
		
	}

	@Override
	protected void prepareInsertStatement(CallableStatement statement,
			StateProvince object) throws SQLException {
		statement.setInt(1, object.getCountry().getCountryId());
		statement.setString(2, object.getStateTitle());
	    statement.registerOutParameter(3, Types.NUMERIC);
	    statement.setString(4, object.getStateTitle());
		
	}


	@Override
	protected void prepareUpdateStatement(CallableStatement statement,
			StateProvince object) throws SQLException {
		 statement.setInt(1, object.getStateId());
	     statement.setInt(2, object.getCountry().getCountryId());
	     statement.setString(3, object.getStateTitle());
	     statement.setString(4, object.getStateTitle());

		
	}


	@Override
	protected List<StateProvince> parseResultSet(ResultSet rs)
			throws SQLException {
		// парсинг районов - провинций
		List<StateProvince> statePrs = new ArrayList<>();

		while (rs.next()) {
            int stateId = rs.getInt(STATE_ID);
            String stateTitle = rs.getString(STATE_TITLE);
            int countryId = rs.getInt(COUNTRY_ID);

            Country country = new Country(countryId);

            statePrs.add(new StateProvince(stateId, stateTitle, country ));
       }

		return statePrs;
	}
	
	@Override
    public int create(StateProvince obj) throws SQLException {
        try (CallableStatement statement = connection.prepareCall(getInsertQuery())) {
            prepareInsertStatement(statement, obj);
            statement.executeUpdate();
         
            return statement.getInt(3);
        }
    }

	public List<StateProvince> readByCountryId(int countryId) throws SQLException {
		 try (PreparedStatement statement = connection.prepareStatement(getSelectByCountryIdQuery())) {
	            statement.setInt(1, countryId);
	            ResultSet rs = statement.executeQuery();
	            return parseResultSet(rs);
	        }
	}

	private String getSelectByCountryIdQuery() {
		return queryManager.getQuery(entityName, "select_by_country_id");
	}
	
}
